﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MimeKit;
using MimeKit.Text;
using MKAdminCore.Controllers;
using MKAdminCore.DTO.Web.Common;
using MKAdminCore.ToolKit.Config;
using MKAdminCore.ToolKit.EmailHelper;

namespace MKAdminCore.Areas.Communicate.Controllers
{
    [Area("Communicate")]
    [Route("Communicate/[controller]/[action]")]
    public class EmailController : BaseMvcController
    {
        public IActionResult Index()
        {
            return View(PageViewFilesConfig.CommunicateEmailIndex);
        }

        [HttpPost]
        //[ValidateInput(false)]
        public async Task<IActionResult> SendEmail(MailInfo parameter)
        {
            var result = new Result();
            if (parameter.To == null || parameter.To.Count == 0)
            {
                result.status = false; ;
                result.msg = "请填写收件人邮箱";
            }
            else if (string.IsNullOrEmpty(parameter.Subject))
            {
                result.status = false; ;
                result.msg = "请填写主题";
            }
            else if (string.IsNullOrEmpty(parameter.Body))
            {
                result.status = false; ;
                result.msg = "请填写邮件内容";
            }
            else
            {
                var mailBoxAddress = new List<MailboxAddress>();
                mailBoxAddress.Add(new MailboxAddress(parameter.To[0]));

                await EmailHelper.SendEMailAsync(parameter.Subject, parameter.Body, mailBoxAddress, TextFormat.Html);
            }

            return Ok(result);
        }
    }
}